package com.example.demoface.controller;

import com.alibaba.fastjson2.JSON;
import com.example.demoface.domian.User;
import com.example.demoface.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@CrossOrigin(origins = "*", allowedHeaders = "*")
public class LoginController {
    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/login")
    public Map login(@RequestParam String tele, @RequestParam String passwd) {
        User user = new User();
        user.setPassWd(passwd);
        user.setTele(tele);
        user = userService.selectUsersWithDept(user);
        if (user != null){
            String encodedString = Base64.getEncoder().encodeToString(tele.getBytes());
            redisTemplate.opsForValue().set("token:" + encodedString,user,5, TimeUnit.MINUTES);
            HashMap res = new HashMap();
            res.put("msg","登录成果");
            res.put("token",encodedString);
            return res;
        }
        else {
            HashMap res = new HashMap();
            res.put("msg","登录失败");

            return res;
        }
//        user.getUserId();
//        redisTemplate.opsForValue().set("user_id:" + user.getUserId(), tele + passwd);
//
//
//        // 查询电话号码对应的user_id
//        // set user_id:100002323   (tel+ passwd)
//
//        return  null;
    }
}
